package com.woniu.controller;


import com.alibaba.excel.EasyExcel;
import com.woniu.aspect.Log;
import com.woniu.entity.HttpStatus;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.QueryLogDTO;
import com.woniu.entity.dto.QueryUserDTO;
import com.woniu.pojo.User;
import com.woniu.pojo.UserLog;
import com.woniu.service.ILogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaos
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/log")
@Api(tags = "日志管理")
public class LogController extends BaseController {

    @Autowired
    private ILogService logService;

    @GetMapping("/all")
    @ApiOperation("查询所有")
    public Result listAll(){
        List<UserLog> users = logService.list();
        return toDataResult(users);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public Result findById(@PathVariable Integer id){
        UserLog user = logService.getById(id);
        return toDataResult(user);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除信息")
    public Result delete(@PathVariable("id") Integer id){
        boolean byId = logService.removeById(id);
        return toDataResult(byId);
    }

    @PostMapping("/delete")
    @ApiOperation("批量删除信息")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        boolean byId = logService.removeBatchByIds(ids);
        return toDataResult(byId);
    }


    @PostMapping("/add")
    @ApiOperation("增加成员")
    public Result addUser(@RequestBody UserLog userLog){
        boolean b = logService.save(userLog);
        return new Result(HttpStatus.SUCCESS,"添加成功",b);
    }

    @PostMapping("/update")
    @ApiOperation("修改信息")
    public Result update(@RequestBody UserLog userLog){
        boolean update = logService.updateById(userLog);
        return new Result(HttpStatus.SUCCESS,"修改成功",update);
    }
    @RequiresPermissions("monitor:operlog:query")
    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("分页查询")
    public Result select(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody QueryLogDTO queryLogDTO
    ){
        PageData pageData =logService.search(currentPage,pageSize,queryLogDTO);
        return toDataResult(pageData);
    }

    @ApiOperation("导出数据")
    @PostMapping("/export")
    public void listSearch(@RequestBody(required = false) QueryLogDTO queryLogDTO,
                           HttpServletResponse response) throws IOException {
        PageData pageData =  logService.search(1, 1000, queryLogDTO);
        List<UserLog> users = (List<UserLog>) pageData.getData();
        EasyExcel.write(response.getOutputStream(), User.class).sheet().doWrite(users);
    }

}

